//
//  JieSuanViewController.m
//  JinChengYun
//
//  Created by 金橙云 on 2017/3/3.
//  Copyright © 2017年 金橙云. All rights reserved.
//

#import "JieSuanViewController.h"

#import "TodoHttpManager.h"
#import "CCZTableButton.h"
@interface JieSuanViewController ()

{
    NSString *_titleStr;
}
@property (nonatomic, strong) CCZTableButton *table;
@property(nonatomic,strong)UIView *oneView;
@property(nonatomic,strong)UIView *twoView;
@property(nonatomic,strong)UIView *firstView;
@property(nonatomic,strong)UIButton *wuBtn;
@property(nonatomic,strong)UIButton *chaBtn;
@property(nonatomic,strong)UILabel *yingLab;
@property(nonatomic,strong)UILabel *yiLab;
@property(nonatomic,strong)UILabel *weiLab;
@property(nonatomic,strong)UILabel *ying1Lab;
@property(nonatomic,strong)UILabel *yi1Lab;
@property(nonatomic,strong)UILabel *wei1Lab;
@property(nonatomic,strong)UILabel *ying2Lab;
@property(nonatomic,strong)UILabel *yi2Lab;
@property(nonatomic,strong)UILabel *wei2Lab;
@property(nonatomic,strong)UILabel *yingHLab;
@property(nonatomic,strong)UILabel *yiHLab;
@property(nonatomic,strong)UILabel *weiHLab;
@property(nonatomic,strong)NSMutableArray *wuArr;
@property(nonatomic,strong)NSMutableArray *wuEveryArr;
@property(nonatomic,strong)NSMutableDictionary *jieDic;
@property(nonatomic,strong)NSMutableArray *wuIDArr;






@end

@implementation JieSuanViewController

-(void)viewWillAppear:(BOOL)animated
{
    //物业类型下拉列表
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didTDYongJinWuYeType:) name:TDYongJinWuYeType object:nil];
    //TODO:物业类型下拉列表
    [_manager userYongJinWuYeType];
    
    //结算详情
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didTDYongJieSuanX:) name:TDYongJieSuanX object:nil];
    NSUserDefaults *userDefa = [NSUserDefaults standardUserDefaults];
    
    NSString *uid= [userDefa objectForKey:@"USER_ID"];
 
    [_manager UserGMYongJinJieSuanXwithID:uid withAdviser:nil withWuYeType:nil];

   
    
}
//物业类型下拉列表
-(void)didTDYongJinWuYeType:(NSNotification *)notif
{
    if (notif.object != 0)
    {
        _wuArr = [notif.object objectForKey:@"data"];
        NSLog(@"-------wuArr-----%@",_wuArr);
        for (int i = 0; i<_wuArr.count; i++)
        {
            NSDictionary *dic = _wuArr[i];
            NSLog(@"----dic----%@",dic);
            [_wuEveryArr addObject:dic[@"name"]];
            NSLog(@"-------111111wuEveryArr-----%@",_wuEveryArr);
            [_wuIDArr addObject:dic[@"id"]];
             [self firstOneView];
           // [self setOneView];
        }
    }
}

//结算详情
-(void)didTDYongJieSuanX:(NSNotification *)notifia
{
    if (notifia.object != 0)
    {
        _jieDic = [notifia.object objectForKey:@"data"];
         [self setOneView];
        [self setUpTwoView];
    }
}
-(void)dealloc
{
    
       //物业类型下拉列表
    [[NSNotificationCenter defaultCenter] removeObserver:self name:TDYongJinWuYeType object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:TDYongJieSuanX object:nil];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = BgColor;
    _manager = [TodoHttpManager sharedManager];
    _wuEveryArr = [NSMutableArray array];
    _jieDic = [NSMutableDictionary dictionary];
    _wuIDArr = [NSMutableArray array];
    
 
}

-(NSMutableArray *)wuArr
{
    if (_wuArr)
    {
        return _wuArr;
    }
    _wuArr = [[NSMutableArray alloc] init];
    return _wuArr;
}

-(void)firstOneView
{


   _firstView =  [[UIView alloc] initWithFrame:CGRectMake(0, 2, B_WIDTH, 40)];
    _firstView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_firstView];
    
    UILabel *zuoLab = [[UILabel alloc] init];
    zuoLab.frame = CGRectMake(10, 10, 60, 20);
    zuoLab.text = @"物业类型";
    zuoLab.textAlignment = NSTextAlignmentRight;
    zuoLab.textColor = [UIColor blackColor];
    zuoLab.font = [UIFont systemFontOfSize:12];
    [_firstView addSubview:zuoLab];
    
    _wuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _wuBtn.frame = CGRectMake(80, 8, 150, 25);
    _wuBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    _wuBtn.layer.cornerRadius =5;
    _wuBtn.layer.borderWidth = 0.5;
    [_wuBtn setTitle:@"全部" forState:0];
    [_wuBtn setTitleColor:[UIColor blackColor] forState:0];
    [_wuBtn addTarget:self action:@selector(wuYeTayeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    _wuBtn.layer.borderColor = [UIColor grayColor].CGColor;
    [_firstView addSubview:_wuBtn];
    
    self.table = [[CCZTableButton alloc] initWithFrame:CGRectMake(80, 190, 150, 0)];
    self.table.offsetXOfArrow = -30;
    [self.table addItems:_wuEveryArr exceptItem:@"全部"];
    [self.table selectedAtIndexHandle:^(NSUInteger index, NSString *itemName) {
        
        [_wuBtn setTitle:itemName forState:UIControlStateNormal];
        // NSLog(@"====%ld",index);
        _titleStr = itemName;
        
        NSLog(@"=====22222222===%@",_titleStr);
        
    }];
    
    
    UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake(210, 15, 10, 10)];
    imgV.image = [UIImage imageNamed:@"sanJiao"];
    [_firstView addSubview:imgV];
    
    _chaBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _chaBtn.frame = CGRectMake(250, 8, 60, 25);
    _chaBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    _chaBtn.layer.cornerRadius =5;
    _chaBtn.backgroundColor = [UIColor orangeColor];
    [_chaBtn setTitle:@"查看" forState:0];
    [_chaBtn setTitleColor:[UIColor whiteColor] forState:0];
    [_chaBtn addTarget:self action:@selector(chanKanBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_firstView addSubview:_chaBtn];
}
-(void)setOneView
{
 
    _oneView = [[UIView alloc] initWithFrame:CGRectMake(0, 42, B_WIDTH, 120)];
    _oneView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_oneView];
        for (int a=0; a<3; a++)
        {
            UILabel *lineLab = [[UILabel alloc] init];
            lineLab.backgroundColor = [UIColor grayColor];
            lineLab.frame = CGRectMake(0, 40*a, B_WIDTH, 0.5);
            [_oneView addSubview:lineLab];
            
        }
    NSArray *zuoArr = @[@"应结算",@"已结算",@"未结算"];
    for (int b=0; b<3; b++)
    {
        UILabel *zuoLab = [[UILabel alloc] init];
        zuoLab.frame = CGRectMake(10, 10+40*b, 60, 20);
        zuoLab.text = zuoArr[b];
        zuoLab.textAlignment = NSTextAlignmentRight;
        zuoLab.textColor = [UIColor blackColor];
        zuoLab.font = [UIFont systemFontOfSize:12];
        [_oneView addSubview:zuoLab];
    }

    _yingLab = [[UILabel alloc] initWithFrame:CGRectMake(80, 8, 150, 25)];
    _yingLab.font = [UIFont systemFontOfSize:12];
    _yingLab.layer.cornerRadius =5;
    _yingLab.layer.borderWidth = 0.5;
    _yingLab.text = _jieDic[@"yingjie_money"];
    _yingLab.textAlignment = NSTextAlignmentCenter;
    _yingLab.layer.borderColor = [UIColor grayColor].CGColor;
    [_oneView addSubview:_yingLab];
    
    
    _yiLab = [[UILabel alloc] initWithFrame:CGRectMake(80, 48, 150, 25)];
    _yiLab.font = [UIFont systemFontOfSize:12];
    _yiLab.layer.cornerRadius =5;
    _yiLab.layer.borderWidth = 0.5;
    _yiLab.text = _jieDic[@"yijie_money"];
    _yiLab.textAlignment = NSTextAlignmentCenter;
    _yiLab.layer.borderColor = [UIColor grayColor].CGColor;
    [_oneView addSubview:_yiLab];
    

    _weiLab = [[UILabel alloc] initWithFrame:CGRectMake(80, 88, 150, 25)];
    _weiLab.font = [UIFont systemFontOfSize:12];
    _weiLab.layer.cornerRadius =5;
    _weiLab.layer.borderWidth = 0.5;
    _weiLab.text =_jieDic[@"weijie_money"];
    _weiLab.textAlignment = NSTextAlignmentCenter;
    _weiLab.layer.borderColor = [UIColor grayColor].CGColor;
    [_oneView addSubview:_weiLab];
    

    
    
    
    
}

-(void)setUpTwoView
{
    _twoView = [[UIView alloc] initWithFrame:CGRectMake(0, 166, B_WIDTH, 160)];
    _twoView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_twoView];
    for (int m = 0; m<3; m++)
    {
        UILabel *hengLab = [[UILabel alloc] init];
        hengLab.backgroundColor = [UIColor grayColor];
        hengLab.frame = CGRectMake(0, 40+40*m, B_WIDTH, 0.5);
        [_twoView addSubview:hengLab];
    }
    
    NSInteger AA = B_WIDTH/4;
    for (int n = 0; n<3; n++)
    {
        UILabel *shuLab = [[UILabel alloc] init];
        shuLab.backgroundColor = [UIColor grayColor];
        shuLab.frame = CGRectMake(AA+AA*n, 0, 0.5, 160);
        [_twoView addSubview:shuLab];
    }
    
    NSArray *hengT = @[@"结算次数",@"应结算",@"已结算",@"未结算"];
    for (int x = 0; x<4; x++)
    {
        UILabel *hengTLab = [[UILabel alloc] init];
        hengTLab.frame = CGRectMake(AA*x, 10, AA, 20);
        hengTLab.textColor = [UIColor blackColor];
        hengTLab.font = [UIFont systemFontOfSize:12];
        hengTLab.text = hengT[x];
        hengTLab.textAlignment = NSTextAlignmentCenter;
        [_twoView addSubview:hengTLab];
    }
    
    NSArray *shuT = @[@"结算1",@"结算2",@"合计"];
    for (int y = 0; y<3; y++)
    {
        UILabel *shuTLab = [[UILabel alloc] init];
        shuTLab.frame = CGRectMake(0, 50+40*y, AA, 20);
        shuTLab.textColor = [UIColor blackColor];
        shuTLab.font = [UIFont systemFontOfSize:12];
        shuTLab.text = shuT[y];
        shuTLab.textAlignment = NSTextAlignmentCenter;
        [_twoView addSubview:shuTLab];
    }
    
    _ying1Lab = [[UILabel alloc] initWithFrame:CGRectMake(AA, 50, AA, 20)];
    _ying1Lab.text = _jieDic[@"yingjie_js1_money"];
    _ying1Lab.textColor = [UIColor blackColor];
    _ying1Lab.font = [UIFont systemFontOfSize:12];
    _ying1Lab.textAlignment = NSTextAlignmentCenter;
    [_twoView addSubview:_ying1Lab];
    
    _ying2Lab = [[UILabel alloc] initWithFrame:CGRectMake(AA, 90, AA, 20)];
    _ying2Lab.text = _jieDic[@"yingjie_js2_money"];
    _ying2Lab.textColor = [UIColor blackColor];
    _ying2Lab.font = [UIFont systemFontOfSize:12];
    _ying2Lab.textAlignment = NSTextAlignmentCenter;
    [_twoView addSubview:_ying2Lab];
    
    _yingHLab = [[UILabel alloc] initWithFrame:CGRectMake(AA, 130, AA, 20)];
    _yingHLab.text = _jieDic[@"yingjie_money"];
    _yingHLab.textColor = [UIColor blackColor];
    _yingHLab.font = [UIFont systemFontOfSize:12];
    _yingHLab.textAlignment = NSTextAlignmentCenter;
    [_twoView addSubview:_yingHLab];
    
    _yi1Lab = [[UILabel alloc] initWithFrame:CGRectMake(AA*2, 50, AA, 20)];
    _yi1Lab.text = _jieDic[@"yijie_js1_money"];
    _yi1Lab.textColor = [UIColor blackColor];
    _yi1Lab.font = [UIFont systemFontOfSize:12];
    _yi1Lab.textAlignment = NSTextAlignmentCenter;
    [_twoView addSubview:_yi1Lab];
    
    _yi2Lab = [[UILabel alloc] initWithFrame:CGRectMake(AA*2, 90, AA, 20)];
    _yi2Lab.text = _jieDic[@"yijie_js2_money"];
    _yi2Lab.textColor = [UIColor blackColor];
    _yi2Lab.font = [UIFont systemFontOfSize:12];
    _yi2Lab.textAlignment = NSTextAlignmentCenter;
    [_twoView addSubview:_yi2Lab];

    _yiHLab = [[UILabel alloc] initWithFrame:CGRectMake(AA*2, 130, AA, 20)];
    _yiHLab.text = _jieDic[@"yijie_money"];
    _yiHLab.textColor = [UIColor blackColor];
    _yiHLab.font = [UIFont systemFontOfSize:12];
    _yiHLab.textAlignment = NSTextAlignmentCenter;
    [_twoView addSubview:_yiHLab];
    
    
    _wei1Lab = [[UILabel alloc] initWithFrame:CGRectMake(AA*3, 50, AA, 20)];
    _wei1Lab.text = _jieDic[@"weijie_js1_money"];
    _wei1Lab.textColor = [UIColor blackColor];
    _wei1Lab.font = [UIFont systemFontOfSize:12];
    _wei1Lab.textAlignment = NSTextAlignmentCenter;
    [_twoView addSubview:_wei1Lab];

    
    _wei2Lab = [[UILabel alloc] initWithFrame:CGRectMake(AA*3, 90, AA, 20)];
    _wei2Lab.text = _jieDic[@"weijie_js2_money"];
    _wei2Lab.textColor = [UIColor blackColor];
    _wei2Lab.font = [UIFont systemFontOfSize:12];
    _wei2Lab.textAlignment = NSTextAlignmentCenter;
    [_twoView addSubview:_wei2Lab];
    
    _weiHLab = [[UILabel alloc] initWithFrame:CGRectMake(AA*3, 130, AA, 20)];
    _weiHLab.text =_jieDic[@"weijie_money"];
    _weiHLab.textColor = [UIColor blackColor];
    _weiHLab.font = [UIFont systemFontOfSize:12];
    _weiHLab.textAlignment = NSTextAlignmentCenter;
    [_twoView addSubview:_weiHLab];

    
}
#pragma mark 物业类型
-(void)wuYeTayeBtnClick:(UIButton *)btn
{
    [self.table show];
}
#pragma mark 查看按钮
-(void)chanKanBtnClick:(UIButton *)btn
{
    NSUserDefaults *userDefa = [NSUserDefaults standardUserDefaults];
    
    NSString *uid= [userDefa objectForKey:@"USER_ID"];

    [_manager UserGMYongJinJieSuanXwithID:uid withAdviser:nil withWuYeType:_titleStr];
  

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
